package com.paschino.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.paschino.modal.Category;
import com.paschino.modal.Product;

@Repository
public class ProductDAOImpl extends AbstractDAO implements ProductDAO {

	@Override
	public boolean addProduct(Product product) {
		getSession().saveOrUpdate(product);
		if (product.getId() > 0) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> getProductList() {
		List<Product> productList = new ArrayList<Product>();
		Query query = getSession()
				.createQuery(
						"from Product p where (p.isDelete is null or p.isDelete = false)");
		productList = query.setMaxResults(20).list();
		return productList;
	}

	@Override
	public Product getProduct(int productId) {
		return (Product) getSession().get(Product.class, productId);
	}

	@Override
	public boolean updateProduct(Product product) {
		String name = product.getName();
		getSession().update(product);
		if (name.equals(product.getName())) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> getProductList(int page, int resultPerPage) {
		List<Product> productList = new ArrayList<Product>();
		Query query = getSession().createQuery("from Product p ");
		query.setFirstResult(resultPerPage * (page - 1));
		query.setMaxResults(resultPerPage);
		productList = query.list();
		return productList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> getProductListByCate(Category cate) {
		List<Product> productList = new ArrayList<Product>();
		Query query = getSession()
				.createQuery(
						"from Product p where (p.isDelete is null or p.isDelete = false) and p.categoryId =:cate")
				.setParameter("cate", cate);
		productList = query.list();
		return productList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> getProductListByCateAndIsActive(Category cate) {
		List<Product> productList = new ArrayList<Product>();
		Query query = getSession()
				.createQuery(
						"from Product p where (p.isDelete is null or p.isDelete = false) and p.isActive = true and p.categoryId =:cate")
				.setParameter("cate", cate);
		productList = query.list();
		return productList;
	}

	@Override
	public int deleteProduct(int productID) {
		Query query = getSession()
				.createQuery(
						"update Product p set p.isDelete = true where p.id = :productID");
		query.setParameter("productID", productID);
		int result = query.executeUpdate();
		return result;
	}

	@Override
	public int hideProduct(int productID) {
		Query query = getSession()
				.createQuery(
						"update Product p set p.isActive = false where p.id = :productID");
		query.setParameter("productID", productID);
		int result = query.executeUpdate();
		return result;
	}

	@Override
	public int unhideProduct(int productID) {
		Query query = getSession()
				.createQuery(
						"update Product p set p.isActive = true where p.id = :productID");
		query.setParameter("productID", productID);
		int result = query.executeUpdate();
		return result;
	}
}
